(function ($) {
	var loginInfo
	var notPull,yesPull;
	var notStart = 0,yesStart=0,Limit = 20;
	var notPulling = false,yesPulling=false;
	var notData,yesData;
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!$.os.plus) {
		baseSetup()
	}
	function getNotStatData(){
		if (notPulling) {
			notPull.endPullUpToRefresh()
			return;
		}
		notPulling = true
		
		//parr N^开始日期^结束日期^状态（A\N\Y） A未保存、N已保存、Y已提交
		//input	检索信息 start	首条数据索引 limit	列表行数 role	角色id APPLoginID	用户id nurseid	人员id
		var pdict = {'parr':'N^^^A','input':'','start':notStart,'limit':Limit,'role':loginInfo['loginRoleCodes'],'APPLoginID':loginInfo['loginID'],'nurseid':loginInfo['loginID']}
		//console.log(JSON.stringify(pdict))
		var paramDict = {'method':'web.INMSurveyComm:FindNurseSurveyWrite','params':JSON.stringify(pdict),'type':'RecQuery'}
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				notPull.endPullUpToRefresh();
				notPulling = false
				return;
			}
			
			if (notStart == 0) {
				notData = dataArr;
			}else{
				notData = notData.concat(dataArr)
			}
			updateStatUI(dataArr,'0')
			if (dataArr.length < Limit) {
				notPull.endPullUpToRefresh(true);
			}else{
				notPull.endPullUpToRefresh();
			}
			notStart += dataArr.length;
			notPulling = false
		}, function(errorStr) {
			notPull.endPullUpToRefresh();
			notPulling = false
		});
	}
	
	function getYesStatData(){
		if (yesPulling) {
			yesPull.endPullUpToRefresh()
			return;
		}
		yesPulling = true
		
		//parr N^开始日期^结束日期^状态（A\N\Y） A未保存、N已保存、Y已提交
		//input	检索信息 start	首条数据索引 limit	列表行数 role	角色id APPLoginID	用户id nurseid	人员id
		var pdict = {'parr':'N^^^NY','input':'','start':yesStart,'limit':Limit,'role':loginInfo['loginRoleCodes'],'APPLoginID':loginInfo['loginID'],'nurseid':loginInfo['loginID']}
		var paramDict = {'method':'web.INMSurveyComm:FindNurseSurveyWrite','params':JSON.stringify(pdict),'type':'RecQuery'}
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				yesPull.endPullUpToRefresh();
				yesPulling = false
				return;
			}
			
			if (yesStart == 0) {
				yesData = dataArr;
			}else{
				yesData = yesData.concat(dataArr)
			}
			updateStatUI(dataArr,'1')
			if (dataArr.length < Limit) {
				yesPull.endPullUpToRefresh(true);
			}else{
				yesPull.endPullUpToRefresh();
			}
			yesStart += dataArr.length;
			yesPulling = false
		}, function(errorStr) {
			yesPull.endPullUpToRefresh();
			yesPulling = false
		});
	}
	
	function updateStatUI(dataArr,type){
		var UL;
		if (type == '0') {
			UL = $('#notUL')[0];
			if (notStart == 0) {
				UL.innerHTML = ''
			}
		}else{
			UL = $('#yesUL')[0];
			if (yesStart == 0) {
				UL.innerHTML = ''
			}
		}
		
		var oldCount = UL.querySelectorAll('li').length;
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var status = oneData['Status']
			var statusIcon = 'nur-weitianxie'
			if (status == 'N') { //A未保存、N已保存、Y已提交
				statusIcon = 'nur-statusS'
			}else if(status == 'Y'){
				statusIcon = 'nur-statusR'
			}
			var html = '<div class="liheader"><div class="headerText">'+oneData['PerName']+' '+oneData['SurveyTitle']+'</div></div>'+
				'<div class="oneline"><div class="leftText">调查内容：</div><div class="rightText">'+oneData['SurveyContent']+'</div></div>'+
				'<div class="oneline"><div class="leftText">开始日期：</div><div class="rightText">'+oneData['StDate']+'</div></div>'+
				'<div class="oneline"><div class="leftText">结束日期：</div><div class="rightText">'+oneData['EndDate']+'</div></div>'+
				'<div class="oneline"><div class="leftText">备注说明：</div><div class="rightText">'+oneData['Remarks']+'</div></div>'+
				'<div class="statusIcon"><div class="nurfont '+statusIcon+'"></div></div>'
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			newLi.id = type+'-'+(oldCount+i);
			newLi.setAttribute('data-type',type)
			UL.appendChild(newLi)
			newLi.addEventListener('tap',tableCellTapped)
		}
	}
	
	function tableCellTapped(){
		var idStr = this.id
		var index = idStr.substring(2,idStr.length)
		var type = this.getAttribute('data-type');
		var sourceData
		if (type == '0') {
			sourceData = notData[index]
		}else{
			sourceData = yesData[index]
		}
		var webview = plus.webview.create('nurSatisfiedInfo.html', 'nurSatisfiedInfo', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{'sourceData':sourceData});
		webview.show('fade-in');
		webview.addEventListener('close',resetPullRefresh)
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		loginInfo = app.getState()
		var slider = document.getElementById('slider');
		slider.addEventListener('slide', function(event) {
			var index = event.detail.slideNumber;
			$('#sliderIndicator .mui-indicator[data-index="'+index+'"]')[0].classList.add('mui-active');
		});
		var indicators = document.querySelectorAll('#sliderIndicator .mui-indicator');
		for (var i = 0; i < indicators.length;i++) {
			indicators[i].addEventListener('tap',function(){
				var oldActive = $('#sliderIndicator .mui-indicator.mui-active')[0];
				if (oldActive) {
					if (oldActive == this) {
						return
					}
					oldActive.classList.remove('mui-active')
				}
				var newIndex = parseInt(this.getAttribute('data-index'))
				this.classList.add('mui-active')
				mui('#slider').slider().gotoItem(newIndex,100)
			});
		}
		//未填写
		var notScroll = document.getElementById('notScroll');
		notPull = $(notScroll).pullToRefresh({
			up: {
				callback: function() {
					getNotStatData()
				}
			}
		});
		//已填写
		var yesScroll = document.getElementById('yesScroll');
		yesPull = $(yesScroll).pullToRefresh({
			up: {
				callback: function() {
					getYesStatData()
				}
			}
		});
		getNotStatData()
		getYesStatData()
	}
	function resetPullRefresh(){
		notStart = 0;
		yesStart = 0;
		notData = []
		yesData = []
		if (notPull) {
			notPull.refresh(true)
		}
		getNotStatData()
		if (yesPull) {
			notPull.refresh(true)
		}
		getYesStatData()
		$('#notWrapper').scroll().scrollTo(0,0,100);
		$('#yesWrapper').scroll().scrollTo(0,0,100);
	}
	
})(mui)
/*
	//IsAdvanced:是否高级版		loginCode:用户工号				loginHireDuty:用户职称	loginID:用户id	loginName:用户姓名
	//loginPerID:人员id			loginRoleCodes:用户角色权限		loginWardDesc:病区名称	topRoleDesc:最高角色权限描述
	
	"RowId": "", ----- 护士填写调查报告id
	"SurveyTitle": "HS01账号测试满意度",----调查标题
	"SurveyContent": "护士工作调查",----调查内容描述
	"StDate": "2022-05-23",----开始日期
	"EndDate": "2022-05-29",----结束日期
	"Status": "A",			----状态
	"StatusDesc": "未填写",----状态描述
	"Remarks": "测试发布",----备注
	"SurveyPerson": "1",---- 调查人员
	"PerName": "冯亮", ----护士姓名
	"SurveyDR": "1", ----调查内容id
	"PerID": "9999", ----护士人员id
	"WardDR": "1", ----病区id
	"WardDesc": "CCU", ----病区描述
	"ReleaseDR": "32", ----调查表Id
	"TotalScore": "", ----得分
	"Explain": ""  ----问卷说明
*/